package com.auerdi.ui.fragment.e_my.orders;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.auerdi.R;
import com.auerdi.ui.fragment.e_my.orders.childs.AllOrderFragment;
import com.auerdi.ui.fragment.e_my.orders.childs.OrderBWaitFragment;
import com.example.egghttp.EggLog.L;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;


public class ADOrdersTabFragment extends SupportFragment {
    @BindView(R.id.statusBar)
    View statusBar;
    @BindView(R.id.title_left)
    RelativeLayout titleLeft;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.tab)
    TabLayout tab;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    Unbinder unbinder;
    private TabLayout mTab;
    private ViewPager mViewPager;
    private ArrayList<SupportFragment> childFRagments = new ArrayList();
    private ArrayList<String> titles = new ArrayList();

    public static ADOrdersTabFragment newInstance(Integer page) {

        Bundle args = new Bundle();
        args.putInt("page", page);
        ADOrdersTabFragment fragment = new ADOrdersTabFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.wechat_fragment_tab_order, container, false);
        L.d("hcia", "onCreateView:");
        mTab = (TabLayout) view.findViewById(R.id.tab);
        mViewPager = (ViewPager) view.findViewById(R.id.viewPager);


//        mTab.addTab(mTab.newTab());
//        mTab.addTab(mTab.newTab());
//        mTab.addTab(mTab.newTab());
//        mTab.addTab(mTab.newTab());
//        mTab.addTab(mTab.newTab());
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        titleLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });


    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        titles.add("全部订单");//1
        titles.add("待付款");//2
        titles.add("待成团");//3
        titles.add("待发货");//4
        titles.add("待收货");//5
        titles.add("待评价");//6

        childFRagments.add(AllOrderFragment.newInstance());//1
        childFRagments.add(OrderBWaitFragment.newInstance());//2
        childFRagments.add(AllOrderFragment.newInstance());//3
        childFRagments.add(AllOrderFragment.newInstance());//4
        childFRagments.add(AllOrderFragment.newInstance());//5
        childFRagments.add(AllOrderFragment.newInstance());//6
    }


    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);


        mViewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return childFRagments.get(position);

            }

            @Override
            public int getCount() {
                return childFRagments.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titles.get(position);
            }
        });


        mTab.setupWithViewPager(mViewPager);
        mViewPager.setCurrentItem(getArguments().getInt("page"));

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
